﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BikeUI
{
    public partial class UserControlButtonGroup : UserControl
    {

        private TextBox keyComponent;
        private GroupBox editableGroupBox;
        private BindingSource bindingSource;

        private bool hasError = false;

        public UserControlButtonGroup()
        {
            InitializeComponent();


            tsbAdd.Enabled = true;
            tsbUpdate.Enabled = true;
            tsbDelete.Enabled = true;
            tsbSave.Enabled = false;
            tsbCancel.Enabled = false;
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {

            if (null != bindingSource)
            {
                bindingSource.AddNew();
                bindingSource.MoveLast();
            }

            tsbAdd.Enabled = false;
            tsbUpdate.Enabled = false;
            tsbDelete.Enabled = false;
            tsbSave.Enabled = true;
            tsbCancel.Enabled = true;

            keyComponent.Enabled = true;
            editableGroupBox.Enabled = true;
        }

        private void tsbUpdate_Click(object sender, EventArgs e)
        {
            tsbAdd.Enabled = false;
            tsbUpdate.Enabled = false;
            tsbDelete.Enabled = false;
            tsbSave.Enabled = true;
            tsbCancel.Enabled = true;

            keyComponent.Enabled = false;
            editableGroupBox.Enabled = true;
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            tsbAdd.Enabled = true;
            tsbUpdate.Enabled = true;
            tsbDelete.Enabled = true;
            tsbSave.Enabled = false;
            tsbCancel.Enabled = false;

            keyComponent.Enabled = false;
            editableGroupBox.Enabled = false;
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {

            if (!hasError)
            {
                tsbAdd.Enabled = true;
                tsbUpdate.Enabled = true;
                tsbDelete.Enabled = true;
                tsbSave.Enabled = false;
                tsbCancel.Enabled = false;

                keyComponent.Enabled = false;
                editableGroupBox.Enabled = false;
            }
        }

        private void tsbCancel_Click(object sender, EventArgs e)
        {
            bindingSource.ResetCurrentItem();
            bindingSource.CancelEdit();

            tsbAdd.Enabled = true;
            tsbUpdate.Enabled = true;
            tsbDelete.Enabled = true;
            tsbSave.Enabled = false;
            tsbCancel.Enabled = false;

            keyComponent.Enabled = false;
            editableGroupBox.Enabled = false;
        }

        public bool HasError
        {
            get { return hasError; }
            set { hasError = value; }
        }

        public TextBox KeyComponent
        {
            get { return keyComponent; }
            set { keyComponent = value; }
        }

        public GroupBox EditableGroupBox
        {
            get { return editableGroupBox; }
            set { editableGroupBox = value; }
        }

        public BindingSource BindingSource
        {
            get { return bindingSource; }
            set { bindingSource = value; }
        }

        public ToolStripButton ButtonAddNew
        {
            get { return tsbAdd; }
            set { tsbAdd = value; }
        }

        public ToolStripButton ButtonUpdate
        {
            get { return tsbUpdate; }
            set { tsbUpdate = value; }
        }

        public ToolStripButton ButtonDelete
        {
            get { return tsbDelete; }
            set { tsbDelete = value; }
        }

        public ToolStripButton ButtonSave
        {
            get { return tsbSave; }
            set { tsbSave = value; }
        }

        public ToolStripButton ButtonCancel
        {
            get { return tsbCancel; }
            set { tsbCancel = value; }
        }
    }
}
